package com.yuufa.exam;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.net.URL;

public class GetExamTest {
    
    public static void main(String[] args) throws Exception {
        
        String[] s = {"程式設計概要", "計算機概要", "資料處理概要", "資訊管理概要", "法學知識與英文"};
        
        for(int i=0;i<s.length;i++) {
            get(s[i]);
        }
    }

    public static void get(String subject) throws Exception {
        
        BufferedReader br = new BufferedReader(new FileReader(new File("e:\\ppt\\" + subject + "\\" + subject + ".txt")));
        FileOutputStream out = null;
        
        String line = "";
        int num = 1;
        
        while( (line = br.readLine()) != null) {
            int startIndex = line.indexOf("Question");
            int endIndex = line.indexOf(".pdf");
            
            if(startIndex != -1 && endIndex != -1) {
                String url = "http://wwwc.moex.gov.tw/ExamQuesFiles/" + line.substring(startIndex, endIndex) + ".pdf";
                
                URL url1 = new URL(url);
                InputStream in = url1.openStream();
                
                out = new FileOutputStream(new File("e:\\ppt\\" + subject + "\\" + subject + "_" + num++ + ".pdf"));
                
                int read = 0;
                byte[] bytes = new byte[512];
                
                while( (read = in.read(bytes)) != -1) {
                    out.write(bytes, 0 , read);
                }
                
                out.close();
                in.close();
            }
        }
        
        br.close();
    }

}
